Ext.define('CS.panel.Grid', {
	extend: 'Ext.grid.GridPanel',
	requires: ['CS.data.CustomStore', 'CS.tool.PagingToolbar', 'CS.plugin.GridRender'],

	columnLines: true,

	constructor: function(config) {
		var me = this;

		config = CS.apply({}, config);

		config.plugins.push('gridrender');

		config.columns = [];
		config.columns.push({
			xtype: 'rownumberer',
			width: 70,
			text: '序号',
			align: 'center'
		});

		if (CS.isNotEmpty(config.fields)) {
			var obj = CS.transKeyValue(config.fields);

			config.items = [];

			for (param in obj) {
				config.items.push({
					xtype: 'field.text',
					fieldLabel: obj[param],
					clsParam: param
				});
			}

			delete config.fields;
		}

		var fields = [];
		Ext.each(config.items, function(item) {
			var col = {};

			col.text = item.fieldLabel;
			col.dataIndex = item.clsParam;

			item.hideLabel = true;
			col.field = CS.create(item);
			col.renderer = me.render;

			if (CS.isNotNull(item.colAlign)) {
				col.align = item.colAlign;
			} else {
				col.align = 'center';
			}

			if (CS.isNotNull(item.colWidth)) {
				col.width = item.colWidth;
			} else {
				col.flex = 1;
			}

			if (CS.isNotNull(item.hidden)) {
				col.hidden = item.hidden;
			}

			config.columns.push(col);
			fields.push(item.clsParam);
		});
		delete config.items;

		config.isLoad = CS.getBoolean(config.isLoad);

		var lastParams = {
			params: config.extendParam
		};
		if (config.paging) {
			config.store = Ext.create('CS.data.CustomStore', {
				url: config.url,
				fields: fields,
				pageSize: config.pageSize,
				lastParams: lastParams,
				autoLoad: config.isLoad ? lastParams : false
			});

			config.bbar = Ext.create('CS.tool.PagingToolbar', {
				store: config.store,
				pageSize: config.pageSize
			});
		} else {
			config.store = Ext.create('CS.data.CustomStore', {
				url: config.url,
				fields: fields,
				lastParams: lastParams,
				autoLoad: config.isLoad ? lastParams : false
			});
		}

		me.callParent([config]);
	},

	render: function(value, metaData) {
		var field = metaData.column.field;

		return this.renderField.apply(this, [value, field]);
	},

	renderField: function(value) {
		return value;
	},

	reload: function(params, callbackFn) {
		var me = this;

		me.store.lastParams = {
			params: CS.apply(params || {}, me.extendParam),
			callback: callbackFn
		};

		me.store.loadPage(1, me.store.lastParams);
	},

	getCurrentParam: function() {
		return this.store.lastParams.params;
	},

	getSelected: function(notNeedCls) {
		var me = this, selected = me.getSelectionModel().getSelection(), clsName = me.clsName, data = null;

		if (selected.length > 0) {
			data = selected[0].getData();
		}

		return CS.getPackageData(data, clsName, notNeedCls);
	},

	getSelection: function(notNeedCls) {
		var me = this, selected = me.getSelectionModel().getSelection(), clsName = me.clsName, data = [];

		Ext.each(selected, function(record) {
			data.push(record.getData());
		});

		return CS.getPackageData(data, clsName, notNeedCls);
	},

	getData: function(notNeedCls) {
		var me = this, data = [], clsName = me.clsName, records = me.store.getRange();

		Ext.each(records, function(record) {
			data.push(record.getData());
		});

		return CS.getPackageData(data, clsName, notNeedCls);
	},

	setData: function(data, notNeedCls) {
		var me = this;

		me.store.removeAll();

		if (notNeedCls != true) {
			data = data[me.clsName];
		}

		Ext.each(data, function(one) {
			me.store.add(one);
		});
	}

});
